Page({
  onShareAppMessage() {
    return {
      title: '',
      path: 'packageComponent/pages/media/live-player/live-player'
    }
  },
  data: {
    theme: 'light',
    videoSrc: 'rtmp://live-bvc.bilivideo.com/live-bvc/',
    // 添加每个方法的调用结果字段
    playResult: '',
    pauseResult: '',
    stopResult: '',
    resumeResult: '',
    muteResult: ''
  },
  onReady() {
    this.ctx = wx.createLivePlayerContext('player')
  },
  handleScanQRCode() {
    wx.scanCode({
      complete: (res) => {
        const {result} = res
        this.setData({
          videoSrc: result
        })
      },
    })
  },
  handleLivePlayerStateChange(e) {
    console.log('live-player code:', e.detail.code)
  },
  handleLivePlayerError(e) {
    console.error('live-player error:', e.detail.errMsg)
  },
  handlePlay() {
    this.ctx.play({
      success: () => {
        console.log('play success')
        this.setData({
          playResult: '播放成功'
        })
      },
      fail: (err) => {
        console.log('play fail')
        this.setData({
          playResult: `播放失败: ${err?.errMsg || '未知错误'}`
        })
      }
    })
  },
  handlePause() {
    this.ctx.pause({
      success: () => {
        console.log('pause success')
        this.setData({
          pauseResult: '暂停成功'
        })
      },
      fail: (err) => {
        console.log('pause fail')
        this.setData({
          pauseResult: `暂停失败: ${err?.errMsg || '未知错误'}`
        })
      }
    })
  },
  handleStop() {
    this.ctx.stop({
      success: () => {
        console.log('stop success')
        this.setData({
          stopResult: '停止成功'
        })
      },
      fail: (err) => {
        console.log('stop fail')
        this.setData({
          stopResult: `停止失败: ${err?.errMsg || '未知错误'}`
        })
      }
    })
  },
  handleResume() {
    this.ctx.resume({
      success: () => {
        console.log('resume success')
        this.setData({
          resumeResult: '恢复成功'
        })
      },
      fail: (err) => {
        console.log('resume fail')
        this.setData({
          resumeResult: `恢复失败: ${err?.errMsg || '未知错误'}`
        })
      }
    })
  },
  handleMute() {
    this.ctx.mute({
      success: () => {
        console.log('mute success')
        this.setData({
          muteResult: '静音成功'
        })
      },
      fail: (err) => {
        console.log('mute fail')
        this.setData({
          muteResult: `静音失败: ${err?.errMsg || '未知错误'}`
        })
      }
    })
  },
  handleVideoSrcInput(e) {
    this.setData({
      videoSrc: e.detail.value
    })
  },
  onLoad() {
    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light'
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  }
})
